package com.hg.oa.module.daka.layer;

import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.transition.Explode;
import android.transition.Transition;
import android.util.Log;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.hg.oa.R;
import com.hg.oa.common.BaseMapActivity;
import com.hg.oa.module.daka.model.LocationRec;
import com.hg.oa.module.daka.presenter.DakaPresenter;
import com.hg.oa.util.LocationUtils;
import com.hg.oa.util.PermissionsUtil;
import com.hg.oa.util.ViewUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by fengxiang on 2016/9/21.
 */
public class DakaActivity extends BaseMapActivity {

    String tag = "DakaActivity";

    Unbinder unbinder;

    @BindView(R.id.ll1)
    LinearLayout ll1;//上班

    @BindView(R.id.ll2)
    LinearLayout ll2; //下班


    @BindView(R.id.img_complete1)
    ImageView img_complete1;

    @BindView(R.id.img_complete2)
    ImageView img_complete2;

    @BindView(R.id.text_morning_address)
    TextView text_morning_address;

    @BindView(R.id.text_afternoon_address)
    TextView text_afternoon_address;

    @BindView(R.id.text_current_morning_time)
    TextView text_current_morning_time;

    @BindView(R.id.text_current_afternoon_time)
    TextView text_current_afternoon_time;


    @BindView(R.id.btn_daka1)
    LinearLayout btn_daka1;

    @BindView(R.id.btn_daka2)
    LinearLayout btn_daka2;

    @BindView(R.id.bmapView)
    MapView bmapView;

    DakaPresenter dakaPresenter;
    //是否是上午
    boolean isMorning = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_daka);
        dakaPresenter = new DakaPresenter(this);
        unbinder = ButterKnife.bind(this);
        super.setMapView(bmapView);
        setBtnLeft1Enable(true);
        setTitle("打卡");
        initLocation();
        isMorning = dakaPresenter.isMorningOrAfternoon(ll1, ll2);
    }



    GeoCoder mSearch = null;

    void initLocation() {
        mSearch = GeoCoder.newInstance();
        mSearch.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {

            @Override
            public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {

            }

            @Override
            public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
                dakaPresenter.onGetReverseGeoCodeResult(baiduMap, result);
            }
        });

        LocationUtils.register(new BDLocationListener() {
            @Override
            public void onReceiveLocation(BDLocation location) {
                LocationRec locationRec = dakaPresenter.onReceiveLocation(this, location, mSearch);
                dakaPresenter.setValue(isMorning, locationRec, text_current_morning_time
                        , text_morning_address, text_current_afternoon_time, text_afternoon_address);
            }
        });

        dakaPresenter.checkAndRequestLocPermission(DakaActivity.this);
    }

    @OnClick(R.id.btn_daka1)
    void daka1() {
        ViewUtil.show(img_complete1);
        ViewUtil.hide(btn_daka1);
    }

    @OnClick(R.id.btn_daka2)
    void daka2() {
        ViewUtil.hide(btn_daka2);
        ViewUtil.show(img_complete2);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            // requestCode即所声明的权限获取码，在checkSelfPermission时传入
            case PermissionsUtil.ACCESS_LOCATION_CODE:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.e(tag, "授权成功");
                    LocationUtils.start();
                } else {
                    Log.e(tag, "授权失败");
                }
                break;
            default:
                break;
        }
    }
}
